<?php
/**
 * Created by PhpStorm.
 * User: chen
 * Date: 18-5-26
 * Time: 下午5:34
 */

namespace App\Http\Controllers\Core;


use App\Http\Controllers\Controller;
use App\Model\Common\Module;
use App\ServiceHandler\Common\ModuleService;
use App\Tool\Error;
use Illuminate\Http\Request;

class ModuleController extends Controller
{
    /**
     * 添加
     * @param Request $request
     * @return \Illuminate\Http\JsonResponse
     */
    public function addModule(Request $request){
        $result = ModuleService::post($request->all());
        if($result === true){
            return $this->success();
        }
        return $this->error(Error::MODULE_ADD_FAILD, $result->all());
    }

    /**
     * 编辑
     * @param Request $request
     * @return \Illuminate\Http\JsonResponse
     */
    public function editModule(Request $request){
        $result = ModuleService::post($request->all(), $request->id);
        if($result === true){
            return $this->success();
        }
        return $this->error(Error::MODULE_ADD_FAILD, $result->all());
    }

    /**
     * 删除
     * @param Request $request
     * @return \Illuminate\Http\JsonResponse
     */
    public function deleteModule(Request $request){
        $mId = Module::getDownInfo($request->id);
        if ($mId) {
            return $this->error(Error::MODULE_DEL_ALREADY);
        }
        $result = Module::destroy($request->id);
        if($result){
            return $this->success();
        }
        return $this->error(Error::MODULE_DEL_FAILD);
    }

    /**
     * 查询
     * @param Request $request
     * @return \Illuminate\Http\JsonResponse
     */
    public function oneModule(Request $request){
        $result = Module::find($request->id);
        if($result){
            return $this->success( Error::OK, $result);
        }
        return $this->error(Error::MODULE_ONE_FAILD);
    }

    /**
     * 查询列表
     * @return \Illuminate\Http\JsonResponse
     */
    public function getModuleList(){
        return $this->success(Error::OK, ModuleService::lists());
    }
}